"""
案例：笔趣阁批量小说下载
"""
import re
import requests

base_url = 'https://www.22biqu.com'
url = 'https://www.22biqu.com/fenlei/1_1.html'

# 解析出每本小说单独的链接地址

article_response = requests.get(url, proxies="").text
# print(article_response)
article_urls = re.findall(r'<span class="s2"><a href="(/biqu\d+/)">', article_response)
# print(article_urls)

# 通过 for 循环遍历每本小说单独的链接

for article_url in article_urls:
    response = requests.get(base_url + article_url).text

    # 匹配小说具体信息：标题、作者、更新时间、小说章节 的链接

    article_title = re.findall(r'<h1>(.*?)</h1>', response)

    article_author = re.findall(r'<p>作&nbsp;&nbsp;者：(.*?)</p>', response)

    article_update_time = re.findall(r'<p>更新时间：(.*?) </p>', response)

    urls = re.findall(fr'<li><a href="({article_url}.*?.html)">', response)

    print('小说的标题:{}'.format(article_title))
    print('小说的作者:{}'.format(article_author))
    print('小说的更新时间:{}'.format(article_update_time))
    print('============================================')
    print('开始下载小说章节')

    for i in urls:
        r = requests.get(base_url + i).text
        title = re.findall(r'<h1 class="title">(.*?)</h1>', r)

        content = re.findall(r'<p>(.*?)</p>', r)
        print('正在下载{}'.format(title))
        content = ' '.join(content)
        if content:
            content = content.replace('\', \'', '')
            content = content.replace('\r', '')
            # print(content)

        # print('============================================')
